package com.study.view.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.study.view.util.UtilView;

import androidx.annotation.Nullable;

/**
 * author : xujianhong
 * date   : 2022/8/23
 * desc   : 仿小米loading
 * version: 1.0
 */
public class RotateProgress extends View {

    private static final String TAG = "RotateProgress";
    //外圈
    Paint outCircle;
    //内圆
    Paint inCircular;

    float strokeWidth = UtilView.sp2px(18);

    //旋转角度
    float rotateAngle = 90;
    //旋转内圆圈的半径
    float inCircularR = UtilView.dip2px(18);

    ValueAnimator valueAnimator = null;

    public RotateProgress(Context context) {
        this(context,null);
    }

    public RotateProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RotateProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        outCircle = initPaint();
        outCircle.setStyle(Paint.Style.STROKE);
        outCircle.setStrokeWidth(strokeWidth);
        inCircular = initPaint();
        inCircular.setStyle(Paint.Style.FILL);
    }

    private Paint initPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float circle = getWidth()/2;

        //绘制外圈
        float left = 0 + strokeWidth/2;
        float top = 0+strokeWidth/2;
        float right = getWidth() - strokeWidth/2;
        float bottom = getHeight() - strokeWidth/2;

        canvas.drawArc( left,  top,  right,  bottom, 0,
        360,false,outCircle);


        float x = (float) (Math.sin(rotateAngle) * (circle - strokeWidth/2 - inCircularR-30) );

        float y = (float) (Math.cos(rotateAngle)*(circle - strokeWidth/2- inCircularR-30));
        //绘制内置圆圈
        canvas.drawCircle(circle+x,circle+y,inCircularR,inCircular);

        Log.d(TAG, "onDraw: x"+x+" y"+y);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width,height),Math.min(width,height));
    }

    public void startLoading(){
        valueAnimator = ValueAnimator.ofFloat(1);
        valueAnimator.setDuration(100000);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = (float) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: "+progress);
                rotateAngle = 360 * progress;
                Log.d(TAG, "onAnimationUpdate: "+rotateAngle);
                invalidate();
            }
        });
        valueAnimator.start();
    }

    public void stopAnimator(){
        if(valueAnimator != null){
            valueAnimator.cancel();
            valueAnimator = null;
        }
    }
}
